﻿Imports System.Windows.Forms
Imports System.IO
Imports System.Data.SqlClient
Public Class Form1
    Dim tenFileSuDung As String = ""
    Dim prcss As Process
    Dim tenFileGrid As String = ""
    Dim dirTemp As String = ""

    Private Sub Buttonopen_Click(sender As Object, e As EventArgs) Handles Buttonopen.Click
        Dim openFileDialog1 As New OpenFileDialog()
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(openFileDialog1.FileName)

            'openFileDialog1.FileName = ""
            'openFileDialog1.ShowDialog()
            'If OpenFileDialog.FileName = "openFileDialog1" Then
            '    openFileDialog1.FileName = ""
            'End If
            TextBoxduongdan.Text = openFileDialog1.FileName
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Response As DialogResult

        OpenFileDialog1.FileName = ""

        OpenFileDialog1.Filter = "PDF Files(*.pdf)|*.pdf|All Files(*.*)|*.*"

        Response = OpenFileDialog1.ShowDialog()

        If Response <> Windows.Forms.DialogResult.Cancel Then
            If OpenFileDialog1.FileName <> "" Then

                'WebBrowser1.Navigate(OpenFileDialog1.FileName)

            End If
        End If


    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        OpenFileDialog2.Filter = "PDF Files(*.pdf)|*.pdf|All Files(*.*)|*.*"
        If OpenFileDialog2.ShowDialog = DialogResult.OK Then
            TextBoxduongdan.Text = OpenFileDialog2.FileName
            AxAcroPDF1.src = OpenFileDialog2.FileName
        End If

        'openfd.InitialDirectory = "C:\"
        'openfd.Title = "Open a PDF file"
        'openfd.Filter = "PDF files|*.pdf"
        'openfd.ShowDialog()
        'TextBox1.Text = openfd.FileName

    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        SavePDFtoDB()
    End Sub
    Private Sub SavePDFtoDB()
        Try
            '  ob_cnn.Open()
            Dim imageData As Byte() = Nothing
            Using fs As New FileStream(TextBoxduongdan.Text, FileMode.Open, FileAccess.Read)
                imageData = New Byte(fs.Length - 1) {}
                fs.Read(imageData, 0, CInt(fs.Length))
            End Using

            Dim Sql As String = ""
            Sql = "INSERT INTO luufile (luunoidung,anh )VALUES(@luunoidung,  @anh)"
            Dim sqlquery As New SqlCommand(Sql, ob_cnn)
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@luunoidung", TextBoxduongdan.Text))
            ' sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@phanmorong", ".pdf"))
            sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@anh", imageData))
            sqlquery.ExecuteNonQuery()
            ' ob_cnn.Close()
            MsgBox("Đã lưu thành công  !!")
        Catch err As Exception
            MsgBox(err.Message)
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class
 